import 'package:flutter/foundation.dart';

/// 修改用户个人信息请求模型
class UpdateUserProfileReq {
  /// 昵称
  final String? nickName;
  
  /// 邮箱
  final String? email;
  
  /// 手机号
  final String? phonenumber;
  
  /// 性别（0男 1女 2未知）
  final String? sex;
  
  /// 头像URL
  final String? avatar;

  const UpdateUserProfileReq({
    this.nickName,
    this.email,
    this.phonenumber,
    this.sex,
    this.avatar,
  });

  /// 转换为JSON（智能变更模式：只包含有值的字段）
  Map<String, dynamic> toJson() {
    final Map<String, dynamic> json = {};
    
    // 🎯 只添加真正需要更新的字段
    if (nickName != null && nickName!.isNotEmpty) {
      json['nickName'] = nickName;
    }
    if (email != null && email!.isNotEmpty) {
      json['email'] = email;
    }
    if (phonenumber != null && phonenumber!.isNotEmpty) {
      json['phonenumber'] = phonenumber;
    }
    if (sex != null && sex!.isNotEmpty) {
      json['sex'] = sex;
    }
    if (avatar != null && avatar!.isNotEmpty) {
      json['avatar'] = avatar;
    }
    
    debugPrint('🎯 智能变更请求体: $json');
    return json;
  }

  /// 从变更Map创建实例（智能变更模式）
  factory UpdateUserProfileReq.fromChanges(Map<String, String?> changes) {
    debugPrint('🎯 UpdateUserProfileReq.fromChanges: 构建智能变更请求');
    debugPrint('📝 变更字段: ${changes.keys.join(', ')}');
    
    return UpdateUserProfileReq(
      nickName: changes.containsKey('nickName') ? changes['nickName'] : null,
      email: changes.containsKey('email') ? changes['email'] : null,
      phonenumber: changes.containsKey('phonenumber') ? changes['phonenumber'] : null,
      sex: changes.containsKey('sex') ? changes['sex'] : null,
      avatar: changes.containsKey('avatar') ? changes['avatar'] : null,
    );
  }

  /// 从JSON创建实例
  factory UpdateUserProfileReq.fromJson(Map<String, dynamic> json) {
    return UpdateUserProfileReq(
      nickName: json['nickName']?.toString(),
      email: json['email']?.toString(),
      phonenumber: json['phonenumber']?.toString(),
      sex: json['sex']?.toString(),
      avatar: json['avatar']?.toString(),
    );
  }

  @override
  String toString() {
    return 'UpdateUserProfileReq(nickName: $nickName, email: $email, phonenumber: $phonenumber, sex: $sex, avatar: $avatar)';
  }
}
